package com.tkjt.pms.modules.tkindexinfo.controller;

import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.tkjt.pms.common.entity.TkIndexInfo;
import com.tkjt.pms.modules.common.service.TkIndexInfoService;
import com.tkjt.pms.modules.common.service.impl.TkIndexInfoServiceImpl;
import com.tkjt.pms.modules.tkindexinfo.vo.TkIndexInfoVO;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import com.tkjt.pms.modules.tkindexinfo.service.IndexInfoService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

/**
 * @Description: tk_index_info
 * @Author: jeecg-boot
 * @Date:   2020-02-14
 * @Version: V1.0
 */
@RestController
@RequestMapping("/tkindexinfo/tkIndexInfo")
@Slf4j
public class TkIndexInfoController extends JeecgController<TkIndexInfo, TkIndexInfoService> {
	@Autowired
	private IndexInfoService indexInfoService;
    @Autowired
    private TkIndexInfoService tkIndexInfoService;
	
	/**
	 * 分页列表查询
	 *
	 * @param tkIndexInfoVO
	 * @param req
	 * @return
	 */
	@GetMapping(value= "/list")
	public Result<?> queryPageList(TkIndexInfoVO tkIndexInfoVO, @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,HttpServletRequest req) {
        tkIndexInfoVO.setPageNo(pageNo);
        tkIndexInfoVO.setPageSize(pageSize);
		return indexInfoService.list(tkIndexInfoVO);
	}
	
	/**
	 *   添加
	 *
	 * @param tkIndexInfo
	 * @return
	 */
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody TkIndexInfo tkIndexInfo) {
		QueryWrapper<TkIndexInfo> queryWrapper = new QueryWrapper<>();
		queryWrapper.lambda().eq(TkIndexInfo::getIndexValueCode,tkIndexInfo.getIndexValueCode());
		List<TkIndexInfo> list = tkIndexInfoService.list(queryWrapper);
		if(list.size() > 0 ){
			return Result.error("指标值编码已存在！");
		}
		tkIndexInfoService.save(tkIndexInfo);
		return Result.ok("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param tkIndexInfo
	 * @return
	 */
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody TkIndexInfo tkIndexInfo) {
		tkIndexInfoService.updateById(tkIndexInfo);
		return Result.ok("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		tkIndexInfoService.removeById(id);
		return Result.ok("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.tkIndexInfoService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.ok("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		TkIndexInfo tkIndexInfo = tkIndexInfoService.getById(id);
		if(tkIndexInfo==null) {
			return Result.error("未找到对应数据");
		}
		return Result.ok(tkIndexInfo);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param tkIndexInfo
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, TkIndexInfo tkIndexInfo) {
        return super.exportXls(request, tkIndexInfo, TkIndexInfo.class, "tk_index_info");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, TkIndexInfo.class);
    }

}
